package com.tuo51.evaluate.controller;


import com.tuo51.evaluate.domain.appusers.Vip;
import com.tuo51.evaluate.service.appuser.ProductService;
import com.tuo51.evaluate.service.appuser.VipService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/eapi/product")
@Api(value = "/eapi/product", tags = "测评 - 产品列表")
public class ProductController {

    @Autowired
    private VipService vipService;

    @Autowired
    private ProductService productService;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/vip/list", method = GET, produces = "application/json")
    public StatusResponse vipList(@RequestHeader("ticket") String ticket) {

        String userId = cache.getStaffId(ticket);

        List<Vip> vipList = vipService.getAllVipWithoutHidden();
        return StatusResponse.success(vipList);
    }

    @RequestMapping(value = "/vip/preview_promo_code", method = POST, produces = "application/json")
    public StatusResponse promoCodeResult(@RequestHeader("ticket") String ticket,
                                          @RequestParam("productId") long productId,
                                          @RequestParam("promoCode") String code){
        return StatusResponse.success(productService.promoCodePreview(productId, code));
    }

    @RequestMapping(value = "/preview_redeem_code", method = POST, produces = "application/json")
    public StatusResponse previewRedeemCode(@RequestHeader("ticket") String ticket,
                                          @RequestParam("redeemCode") String code){
        String userId = cache.getStaffId(ticket);

        Object result = productService.redeemCodePreview(code);

        return StatusResponse.success(result);
    }


    @RequestMapping(value = "/redeem", method = GET, produces = "application/json")
    public StatusResponse redeem(@RequestHeader("ticket") String ticket, @RequestParam("redeemCode") String code ) {

        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(productService.redeemCodeIssue(staffId, code));
    }
}
